package Program.UserUI;

import Program.Information.User;
import Program.SqlTool.AdminDAO;
import Program.SqlTool.UserDAO;
import Program.Tool.Popup;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

public class RechargePaneSon extends JPanel {
    private User user;
    //日期
    private String date;
    //充值按钮
    private JButton rechargeJButton;
    //清空按钮
    private JButton clearJButton;
    //刷新按钮
    private JButton refreshJButton;
    //设置字体
    private Font font = new Font("微软雅黑", Font.BOLD, 20);
    //金额输入框
    private JTextField amountField;

    public RechargePaneSon(User user) {
        super(null);
        setBounds(0, 0, 900, 600);
        setBackground(Color.WHITE);
        this.user = user;
        initField();
        initJButton();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(new ImageIcon("Application_images\\userBG2.png").getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
    }

    private void initField() {
        //日期标签
        JLabel dateJLabel = new JLabel("日  期");
        dateJLabel.setBounds(260, 80, 100, 50);
        dateJLabel.setFont(font);
        this.add(dateJLabel);
        //日期内容标签
        date = getDateStr();
        JLabel dateContentJLabel = new JLabel(date, JLabel.CENTER);
        dateContentJLabel.setBounds(400, 80, 120, 50);
        dateContentJLabel.setOpaque(true);
        dateContentJLabel.setBackground(Color.lightGray);
        dateContentJLabel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        dateContentJLabel.setFont(font);
        this.add(dateContentJLabel);

        //业主姓名标签
        JLabel nameJLabel = new JLabel("业主姓名");
        nameJLabel.setBounds(250, 160, 120, 50);
        nameJLabel.setFont(font);
        this.add(nameJLabel);
        //业主姓名内容标签
        JLabel ncJLabel = new JLabel(user.getName(), JLabel.CENTER);
        ncJLabel.setBounds(400, 160, 100, 50);
        ncJLabel.setOpaque(true);
        ncJLabel.setBackground(Color.lightGray);
        ncJLabel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        ncJLabel.setFont(font);
        this.add(ncJLabel);

        //账号标签
        JLabel userNameJLabel = new JLabel("用户名");
        userNameJLabel.setBounds(260, 240, 100, 50);
        userNameJLabel.setFont(font);
        this.add(userNameJLabel);
        //账号内容标签
        JLabel uncJLabel = new JLabel(user.getUserName(), JLabel.CENTER);
        uncJLabel.setBounds(400, 240, 220, 50);
        uncJLabel.setOpaque(true);
        uncJLabel.setBackground(Color.lightGray);
        uncJLabel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        uncJLabel.setFont(font);
        this.add(uncJLabel);

        //充值金额标签
        JLabel amountJLabel = new JLabel("充值金额");
        amountJLabel.setBounds(250, 320, 100, 50);
        amountJLabel.setFont(font);
        this.add(amountJLabel);
        //充值金额输入框
        amountField = new JTextField();
        amountField.setBounds(400, 320, 220, 50);
        amountField.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        amountField.setFont(font);
        this.add(amountField);

    }

    //初始化按钮
    private void initJButton() {
        //添加按钮
        rechargeJButton = new JButton("充值");
        rechargeJButton.setFont(font);
        rechargeJButton.setBounds(420, 450, 80, 65);
        if (user.getState().equals("0")) {
            // 账号不可用的情况下，无法正常使用添加功能
            rechargeJButton.setBackground(Color.DARK_GRAY);
            rechargeJButton.setEnabled(false);
            rechargeJButton.setToolTipText("您的账户不可用,请联系管理员");
        } else {
            rechargeJButton.setBackground(new Color(235, 135, 112));
            rechargeJButton.setEnabled(true);
        }
        //取消按钮的焦点框
        rechargeJButton.setFocusPainted(false);
        //取消按钮的边框
        rechargeJButton.setBorder(null);
        //设置鼠标样式
        rechargeJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        //清空按钮
        clearJButton = new JButton("清空");
        clearJButton.setFont(font);
        clearJButton.setBounds(540, 450, 80, 65);
        clearJButton.setBackground(new Color(191, 230, 240));
        //取消按钮的焦点框
        clearJButton.setFocusPainted(false);
        //取消按钮的边框
        clearJButton.setBorder(null);
        //设置鼠标样式
        clearJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        //刷新按钮
        refreshJButton = new JButton("刷新");
        refreshJButton.setFont(font);
        refreshJButton.setBounds(300, 450, 80, 65);
        refreshJButton.setBackground(new Color(103, 172, 80));
        //取消按钮的焦点框
        refreshJButton.setFocusPainted(false);
        //取消按钮的边框
        refreshJButton.setBorder(null);
        //设置鼠标样式
        refreshJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        //获取监听器
        ActionListener listener = getListener();
        //按钮添加监听
        rechargeJButton.addActionListener(listener);
        clearJButton.addActionListener(listener);
        refreshJButton.addActionListener(listener);

        //添加按钮
        this.add(rechargeJButton);
        this.add(clearJButton);
        this.add(refreshJButton);
    }

    private ActionListener getListener() {
        ActionListener listener = new ActionListener() {
            //弹窗信息
            String info = "";

            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == rechargeJButton) {
                    String amountTemp = amountField.getText();
                    if (!checkNumber(amountTemp)){
                        info = "请输入正确的充值金额";
                        Popup.showJDialog(info);
                    }else {
                        double amount = Double.parseDouble(amountTemp);
                        if (amountTemp.length() == 0) {
                            info = "请输入充值金额";
                            Popup.showJDialog(info);
                        } else if (amount<=0){
                            info = "请输入正确的充值金额";
                            Popup.showJDialog(info);
                        } else{
                            int i = Popup.showPaymentCode(amountTemp);
                            if (i == 1) {
                                UserDAO.addRecharge(user.getUserID(), date, amount);
                            } else {
                                info = "充值失败";
                                Popup.showJDialog(info);
                            }
                        }
                        amountField.setText("");
                    }
                } else if (e.getSource() == clearJButton) {
                    amountField.setText("");
                } else {
                    refreshJPanel();
                }
            }
        };
        return listener;
    }

    //判断是否为数字
    private boolean checkNumber(String  amountTemp) {
        String regex = "^\\d+(\\.\\d{1,3})?$";
        boolean flag = amountTemp.matches(regex);
        return flag;
    }

    //刷新界面
    private void refreshJPanel() {
        this.removeAll();
        this.repaint();
        this.updateUI();
        user = UserDAO.selectUser(user.getUserName());
        initField();
        initJButton();
    }

    //获取时间
    private String getDateStr() {
        Calendar instance = Calendar.getInstance();
        //3.获取属性
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;//Calendar的月份值是0-11
        int day = instance.get(Calendar.DAY_OF_MONTH);

        return year + "/" + month + "/" + day;
    }
}
